﻿#include <QFile>
#include "CCorrectPointState.h"
#include "CGlobal.h"

CCorrectPointState::CCorrectPointState(CDevDataBeltRealign* pDevDataBeltRealign)
	: m_pDevDataBeltRealign(pDevDataBeltRealign)
{
	ui.setupUi(this);
	m_sNormalLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(120, 120, 120);";
	m_sWorkLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(0, 255, 0);";
	m_sAlarmLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(255, 0, 0);";
	m_sWarningLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(255,255,0);";
	if (m_pDevDataBeltRealign->m_nIsHaveDev==0)
	{
		ui.labelYC->hide();
		ui.labelJD->hide();
		ui.labelRectRemFeedback->hide();
		ui.labelRecLocalFeedback->hide();
	}
	if (m_pDevDataBeltRealign->m_nIsHaveBeltRunState == 0)
	{
		ui.labelBeltState->hide();
		ui.label_7->hide();
	}
	m_bIsInit = false;
	//配置定时器
	m_pTimer = new QTimer(this);
	m_pTimer->setTimerType(Qt::TimerType::PreciseTimer);
	connect(m_pTimer, &QTimer::timeout, this, &CCorrectPointState::slotDataRefresh);
}

CCorrectPointState::~CCorrectPointState()
{}

void CCorrectPointState::setStyleSheetFile()
{
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

void CCorrectPointState::slotDataRefresh()
{
	if (isVisible())
	{
		m_pDevDataBeltRealign->m_pMutex->lock();
		//摄像头状态
		if (m_pDevDataBeltRealign->m_pDevDataCam->m_bLogin == true)
		{
			ui.labelCamerState->setStyleSheet(m_sWorkLamp);
		}
		else
		{
			ui.labelCamerState->setStyleSheet(m_sAlarmLamp);
		}

		//算法状态
		if (m_pDevDataBeltRealign->m_nAlgState == 1)
		{
			ui.labelAlgState->setStyleSheet(m_sWorkLamp);
		}
		else
		{
			ui.labelAlgState->setStyleSheet(m_sAlarmLamp);
		}
		//跑偏状态
		if (m_pDevDataBeltRealign->m_bIsCorrect == true && m_pDevDataBeltRealign->m_bIsCorrectTwo == false)
		{
			ui.labelCorrectState->setStyleSheet(m_sWarningLamp);
		}
		else if (m_pDevDataBeltRealign->m_bIsCorrect == false && m_pDevDataBeltRealign->m_bIsCorrectTwo == true)
		{
			ui.labelCorrectState->setStyleSheet(m_sAlarmLamp);
		}
		else
		{
			ui.labelCorrectState->setStyleSheet(m_sWorkLamp);
		}

		//皮带启停信号
		if (m_pDevDataBeltRealign->m_bBeltWork)
		{
			ui.labelBeltState->setStyleSheet(m_sWorkLamp);
		}
		else
		{
			ui.labelBeltState->setStyleSheet(m_sNormalLamp);
			if (m_pDevDataBeltRealign->m_nIsHaveBeltRunState)
			{
				ui.labelCorrectState->setStyleSheet(m_sNormalLamp);
			}
		}
		ui.labelPointName->setText(m_pDevDataBeltRealign->m_sPointName);
		ui.labelLeft->setText(QString::number(m_pDevDataBeltRealign->m_dLeftCurrVal * 10, 'f', 1));
		ui.labelRight->setText(QString::number(m_pDevDataBeltRealign->m_dRightCurrVal * 10, 'f', 1));
		ui.widget->setDeviationValue(m_pDevDataBeltRealign->m_dLeftCurrVal*10, m_pDevDataBeltRealign->m_dRightCurrVal*10, m_pDevDataBeltRealign->m_nRealignSetValue,
			m_pDevDataBeltRealign->m_nRealignSetValueTwo, m_pDevDataBeltRealign->m_nIsHaveBeltRunState, m_pDevDataBeltRealign->m_bBeltWork);
		m_pDevDataBeltRealign->m_pMutex->unlock();
	}
	m_pDevDataBeltRealign->m_pMutex->lock();
	
	int nFeedback = -1;
	//纠偏远程反馈
	if (m_pDevDataBeltRealign->m_nRecRemoteCtlFeedback == 1)
	{
		ui.labelRectRemFeedback->setStyleSheet(m_sWorkLamp);
		nFeedback = 1;
	}
	else
	{
		ui.labelRectRemFeedback->setStyleSheet(m_sNormalLamp);
	}
	//纠偏本地反馈
	if (m_pDevDataBeltRealign->m_nRecLocalCtlFeedback == 1)
	{
		ui.labelRecLocalFeedback->setStyleSheet(m_sWorkLamp);
		nFeedback = 0;
	}
	else
	{
		ui.labelRecLocalFeedback->setStyleSheet(m_sNormalLamp);
	}
	m_pDevDataBeltRealign->m_pMutex->unlock();
	//本地远程切换，生成日志记录
	if (!m_bIsInit)
	{
		//首次打开软件，默认将未闭环的记录闭环
		QString sErrMsg = "";
		//CSqlOperAI::instance().updateCorrectStopTimeAll(sErrMsg);
		m_nOldFeedback = nFeedback;
		m_bIsInit = true;
	}
	else
	{
		if (m_nOldFeedback != nFeedback)
		{
			if (nFeedback == 0)
			{
				
			}
			else if (nFeedback == 1)
			{
				
			}
			m_nOldFeedback = nFeedback;
		}
	}
}

void CCorrectPointState::refreshData(bool bRefresh)
{
	if (bRefresh)
	{
		m_pTimer->start(1000);
	}
	else
	{
		m_pTimer->stop();
	}
}
